package com.zy.swagger.controller.sys;

import com.zy.swagger.constant.ProjectConstant;
import com.zy.swagger.util.VerificationCode;
import com.zy.swagger.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * description 登录控制器
 *
 * @author ZY
 * @date 2021年03月30日 23:53
 **/
@Api(tags = "登陆管理")
@RestController
public class LoginController {

    @Value("${login.rememberMe.key:}")
    private String rememberMeKey;

    @GetMapping("/getKey")
    public ResultVO<String> getKey() {
        return ResultVO.getSuccess(rememberMeKey);
    }

    @GetMapping("/login")
    public ResultVO<String> login() {
        return ResultVO.getError(500, "尚未登录，请登录!");
    }

    @GetMapping("/logout")
    public ResultVO<String> logout() {
        return ResultVO.getSuccess();
    }

    @ApiOperation("获取图形验证码")
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        VerificationCode code = new VerificationCode();
        BufferedImage image = code.getImage();
        String text = code.getText();
        HttpSession session = request.getSession(true);
        // 在session中设置验证码，在登录接口中验证前端传的验证码与session中的验证码是否一致
        session.setAttribute(ProjectConstant.SESSION_CODE_KEY, text);
        VerificationCode.output(image,resp.getOutputStream());
    }
}
